package com.xtxb.utils.log.config;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 作者: xtxb-fedora
 * <p>
 * 日期: 2021年01月20日-上午11:48
 * <p>
 * 版权说明：xtxb
 * <p>
 * 负责管理日志配置并实时通知日志输出效果
 */
public class ConfigServer implements  Runnable{
    private List<Configer> list=new ArrayList<>(50);
    private long lasttime=0;

    public ConfigServer(){
        File file=createConf();
        Properties pro=new Properties();
        try {
            pro.load(new FileInputStream(file));
            LoggerConf.setPrint("true".equals(pro.getProperty("print")));
            LoggerConf.setType(Integer.parseInt(pro.getProperty("type")));

        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread t=new Thread(new ThreadGroup("debug_log"),this,"Debug Log Conf Thread");
        t.setDaemon(true);
        t.start();
    }

    public void addConfiger(Configer configer){
        list.add(configer);
    }

    public void fresh(){
        for (int i = 0; i < list.size(); i++) {
            list.get(i).initLogger();
        }
    }

    private File createConf(){
        File file=new File(System.getProperty("user.dir")+File.separator+"log.conf");
        if(!file.exists()){
            Properties pro=new Properties();
            pro.put("print","false");
            pro.put("type","0");
            BufferedWriter out=null;
            try {
                file.createNewFile();
                out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                pro.store(out,"type:  0 - Cosole; 1 - File");
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(out!=null)
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
        return file;
    }

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        File file=null;
        try {
            URL url=getClass().getClassLoader().getResource("log.conf");
            if(url!=null ){
                file=new File(url.toURI());
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        if(file==null)
            file=createConf();
        System.out.println("调试日志配置文件路径[ "+file.toURI()+" ]");
        while (true){
            long time=file.lastModified();
            if(time>lasttime){
                lasttime=time;
                Properties pro=new Properties();
                try {
                    pro.load(new FileInputStream(file));
                    LoggerConf.setPrint("true".equals(pro.getProperty("print")));
                    LoggerConf.setType(Integer.parseInt(pro.getProperty("type")));
                    fresh();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
